import {Request, Response, NextFunction, Router} from "express";
import axios from "axios";

interface BodyRequestBody extends Request {
    body: {
        [key: string]: string | undefined
    }
}


const weatherRegRouterFactory = () => Router()
    .get('/weather-temperature', async (req: BodyRequestBody, res: Response, next: NextFunction) => {
            try {
                const data: any = await new Promise((resolve, reject) => {
                    axios.get('https://devapi.qweather.com/v7/weather/now?key=19917e38a2504e88a86fe7f4809df0e2&location=101230905').then(
                        r => resolve(r.data)
                    )
                })
                if (data) {
                    return res.json({
                        code: 1,
                        data: data,
                        msg:'实时天气'
                    })
                }
                return res.json({
                    errCode: 10001,
                    msg: '实时天气数据获取失败'
                })
            } catch (e) {
                console.log(e);
                res.json({
                    errCode: 10001,
                    msg: '动态数据获取失败'
                })
                next()

            }
        }
    )
    .get('/weather-dressing', async (req: BodyRequestBody, res: Response, next: NextFunction) => {
            try {
                const data: any = await new Promise((resolve, reject) => {
                    axios.get('https://devapi.qweather.com/v7/indices/1d?key=19917e38a2504e88a86fe7f4809df0e2&location=101230905&type=9').then(
                        r => resolve(r.data)
                    )
                })
                if (data) {
                    return res.json({
                        code: 1,
                        data: data,
                        msg: '穿衣指数'
                    })
                }
                return res.json({
                    errCode: 10001,
                    msg: '天气数据获取失败'
                })
            } catch (e) {
                console.log(e);
                res.json({
                    errCode: 10001,
                    msg: '动态数据获取失败'
                })
                next()
            }
        }
    )
    .get('/weather-3D', async (req: BodyRequestBody, res: Response, next: NextFunction) => {
            try {
                const data: any = await new Promise((resolve, reject) => {
                    axios.get('https://devapi.qweather.com/v7/weather/3d?location=101230905&key=19917e38a2504e88a86fe7f4809df0e2').then(
                        r => resolve(r.data)
                    )
                })
                if (data) {
                    return res.json({
                        code: 1,
                        data: data,
                        msg: '近三日天气'
                    })
                }
                return res.json({
                    errCode: 10001,
                    msg: '天气数据获取失败'
                })
            } catch (e) {
                console.log(e);
                res.json({
                    errCode: 10001,
                    msg: '动态数据获取失败'
                })
                next()
            }
        }
    )


;
export default weatherRegRouterFactory
